<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends BaseController {
    public function index(){
    	$this->usercount();
    	$this->onlinecount();
    	$this->viewnum();
    	$this->viewlist();
    	$this->loglogin();
    	$this->comfrom();
    	$this->lastWeek();
        $this->display();
    }
    function usercount(){
    	$count=simList(M('User'),'','','count');
    	$this->assign('usercount',$count);
    }
    function onlinecount(){
    	$time=time()-600;
    	$map=array('ip_createtime'=>array("GT",$time));
    	$count=simList(M('Viewip'),$map,'','count');
    	$this->assign('onlinecount',$count);
    }
    function viewnum(){
    	$time=time();
    	$ymd=date('Y-m-d');
    	$time=strtotime($ymd);
    	$map=array('v_createtime'=>array("GT",$time));
    	$count=simList(M('View'),$map,'','count');
    	$this->assign('viewcount',$count);
    }
    function viewlist(){
    	$data=M('View')
    	     ->field(array('v_url','count("v_id")'=>'count'))
    	     ->group('v_url')
    	     ->limit(5)
    	     ->select();
    	$this->assign('vs',$data);
    }
    //登录列表
    function loglogin(){
    	$map=array(
            'log_action'=>'login',
    	);
        $list=simList(M('Log'),$map,'','select','log_createtime',false,5);
        foreach ($list as &$value) {
        	$value['area']=getaddress($value['log_ip']);
        	$value['username']=username($value['user_id']);
        }
        $this->assign('log',$list);
    }
    //用户访问来源
    function comfrom(){
    	$model=M('Viewip');
    	$count['self']=simList($model,array('ip_way'=>'0'),'','count');
    	$count['engine']=simList($model,array('ip_way'=>'engine'),'','count');
    	$count['friend']=simList($model,array('ip_way'=>'1'),'','count');
    	$this->assign('from',$count);
    }
    //最近一周访问
    function lastWeek(){
       $model=M('View');
       $now=time();
       $weekarray=array("日","一","二","三","四","五","六");
       $i=0;
       for($i=0;$i<7;$i++){
           $time=$now-60*60*24*(6-$i);
           $ymdtimebig=date('Y-m-d',$now-60*60*24*(6-$i));
           $ymdtimesmall=date('Y-m-d',$now-60*60*24*(5-$i));
           $timebig=strtotime($ymdtimebig);
           $timesmall=strtotime($ymdtimesmall);

           $map=array(
                 'v_createtime'=>array('BETWEEN',array($timebig,$timesmall)),
           	);
           //var_dump($map);
           $data[$i]['week']='周'.$weekarray[date('w',$time)];
           $data[$i]['count']=simList($model,$map,'','count');
           
       }
       $this->assign('weekc',$data);

    }
}